﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModEmployee
{
    public partial class BankAccountModi : System.Web.UI.Page
    {
        EmployeeBiz employeeBiz = new EmployeeBiz();
        BankAccountBiz bankAccountBiz = new BankAccountBiz();
        string account = "";
        string code = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }

            if (Request.Params["EmpCode"] != null)
            {
                code = Request.Params["EmpCode"].ToString();
               // txtEmpCode.Text = code;
            }
            if (!IsPostBack)
            {
                if (!string.IsNullOrWhiteSpace(code))
                {
                    
                    DataTable dt = employeeBiz.GetOneInfo(code);
                    if (dt.Rows.Count > 0)
                    {
                        txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                        txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtBankName.Text = dt.Rows[0]["SalaryState"].ToString();
                        txtBankAccount.Text = dt.Rows[0]["SalaryAccount"].ToString();
                    }
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmployeeShowDetail.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "EmpList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }

            string empCode = txtEmpCode.Text.Trim();
            string bankName = txtBankName.Text.Trim();
            string bankAccount = txtBankAccount.Text.Trim();

            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "工号不能为空！");
                return;
            }
            SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
            hList.Add("EmpCode", empCode);
            hList.Add("EmpName", txtEmpName.Text.Trim());
            hList.Add("BankName", bankName);
            hList.Add("BankAccount", bankAccount);
           
            if ( bankAccountBiz.Update(hList))
                JSUtility.Alert(this, "更新成功！");
            else
                JSUtility.Alert(this, "更新失败！");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtBankName.Text = dt.Rows[0]["SalaryState"].ToString();
                            txtBankAccount.Text = dt.Rows[0]["SalaryAccount"].ToString();
                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtBankName.Text = dt.Rows[0]["SalaryState"].ToString();
                            txtBankAccount.Text = dt.Rows[0]["SalaryAccount"].ToString();
                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职，不能修改");
                    }
                }
            }
           
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmployeeShowDetail.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "EmpList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }

            string empCode = txtEmpCode.Text.Trim();
            string bankName = txtBankName.Text.Trim();
            string bankAccount = txtBankAccount.Text.Trim();

            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "工号不能为空！");
                return;
            }
            SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
            hList.Add("EmpCode", empCode);
            hList.Add("EmpName", txtEmpName.Text.Trim());
            hList.Add("BankName", bankName);
            hList.Add("BankAccount", bankAccount);

            if (bankAccountBiz.Insert(hList))
                JSUtility.Alert(this, "增加成功！");
            else
                JSUtility.Alert(this, "增加失败！你是不是想更新？");
        }
    }
}